package com.example.demo.service;

import com.example.demo.support.cache.ChannelCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CacheService {
    public Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MybatisDao myBatisDao;

    public Map<String, Object> getGatewayDetailCache(Integer gatewayId) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getGatewayDetail" + "-" + gatewayId);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("gatewayId", gatewayId);
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getGatewayDetail", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getGatewayDetail" + "-" + gatewayId, resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    public Map<String, Object> getGatewayCache(Integer spId) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getGateway" + "-" + spId);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("spId", spId);
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getGateway", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getGateway" + "-" + spId, resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    private Map<String, Object> getChannelCache(Integer channelId) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getChannel" + "-" + channelId);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("channelId", channelId);
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getChannel", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getChannel" + "-" + channelId, resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    public Map<String, Object> getSpTestSwitchCache(String spType) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getSpTestSwitch" + "-" + spType);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("spType", spType);
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getSpTestSwitch", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getSpTestSwitch" + "-" + spType, resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    public Map<String, Object> getSendConfigCache(String gatewayIdStr) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getSendConfigCache" + "-" + gatewayIdStr);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("pkid", Integer.parseInt(gatewayIdStr));
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getSendConfigIfValid", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getSendConfigCache" + "-" + gatewayIdStr, resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    public Map<String, Object> getTaobaoErrCodeCache() {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getTaobaoErrCache");
        if (null == resultMap) {
            List<Map<String, Object>> taobaoErrCodeList = myBatisDao.getList("ChannelMapper.getTaobaoErrCode", null);
            resultMap = new HashMap<>();
            if (taobaoErrCodeList != null && taobaoErrCodeList.size() > 0) {
                for (Map<String, Object> objMap : taobaoErrCodeList) {
                    resultMap.put(objMap.get("err_code").toString(), objMap.get("err_desc").toString());
                }
            }
            if (resultMap.size() > 0) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getTaobaoErrCache", resultMap);
                return resultMap;
            } else {
                return null;
            }
        } else {
            return resultMap;
        }
    }

    public List<String> getEdmTabList(Integer tabType) {
        List<String> edmTablist = ChannelCache.EDM_TAB_LIST_CACHE.getIfPresent("getEdmTabList" + "-" + tabType);
        if (null == edmTablist) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("tabType", tabType);
            edmTablist = myBatisDao.getList("ChannelMapper.getEdmTabList", paramMap);
            if (edmTablist != null && edmTablist.size() > 0) {
                ChannelCache.EDM_TAB_LIST_CACHE.put("getEdmTabList" + "-" + tabType, edmTablist);
                return edmTablist;
            } else {
                return null;
            }
        } else {
            return edmTablist;
        }
    }

    public String getEdmTab(Integer tabType, Integer spId) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getEdmTab" + "-" + tabType + "-" + spId);
        if (null == resultMap) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("tabType", tabType);
            paramMap.put("spId", spId);
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getEdmTab", paramMap);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getEdmTab" + "-" + tabType + "-" + spId, resultMap);
                return resultMap.get("spTab").toString();
            } else {
                return null;
            }
        } else {
            return resultMap.get("spTab").toString();
        }
    }

    public String getChannelNameByGatewayId(Integer gatewayId) {
        String resultValue = null;
        Map<String, Object> gatewayDetailMap = getGatewayDetailCache(gatewayId);
        if (gatewayDetailMap != null) {
            Map<String, Object> gatewayMap = getGatewayCache((Integer) gatewayDetailMap.get("spId"));
            if (gatewayMap != null) {
                Map<String, Object> channelMap = getChannelCache((Integer) gatewayMap.get("channelId"));
                if (channelMap != null) {
                    resultValue = (String) channelMap.get("channelName");
                }
            }
        }
        return resultValue;
    }

    public String getSysConfigCache(String name) {
        Map<String, Object> resultMap = ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.getIfPresent("getSysConfigCache" + "-" + name);
        if (null == resultMap) {
            resultMap = myBatisDao.getSingleRow("ChannelMapper.getSysConfig", name);
            if (resultMap != null) {
                ChannelCache.GATEWAY_CONFIG_RELATED_CACHE.put("getSysConfigCache" + "-" + name, resultMap);
                return (String) resultMap.get("value");
            } else {
                return null;
            }
        } else {
            return (String) resultMap.get("value");
        }
    }

    public List<Map<String, Object>> getIfExistGatewayTestDetail() {
        List<Map<String, Object>> gatewayTestFromAdmin = ChannelCache.GATEWAY_TEST_CACHE.getIfPresent("GatewayTestFromAdmin");
        if (gatewayTestFromAdmin == null) {
            gatewayTestFromAdmin = myBatisDao.getList("ChannelMapper.getGatewayTestDetail", null);
            if (gatewayTestFromAdmin != null) {
                ChannelCache.GATEWAY_TEST_CACHE.put("GatewayTestFromAdmin", gatewayTestFromAdmin);
            }
        }
        return gatewayTestFromAdmin;
    }
}
